package com.shoesdemo.activity;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.shoesdemo.BaseActivity;
import com.shoesdemo.R;
import com.shoesdemo.data.TomorrowData;

import org.litepal.LitePal;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class HistoryActivity extends BaseActivity {

    private RecyclerView mRecyclerView;
    private List<TomorrowData> mData = new ArrayList<>();
    private SwipeRefreshLayout mLayout;
    private int count;
    private HistoryAdapter mHistoryAdapter;
    private LinearLayoutManager mLayoutManager;

    @Override
    protected void initView() {
        mRecyclerView = findViewById(R.id.recyclerview);
        mLayout = findViewById(R.id.swipe_layout);
        setTitle("我的番茄");
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mHistoryAdapter = new HistoryAdapter();
        mRecyclerView.setAdapter(mHistoryAdapter);
    }

    @Override
    protected void initListener() {
        mLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mLayout.setRefreshing(false);
            }
        });

        mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int endCompletelyPosition = mLayoutManager.
                        findLastCompletelyVisibleItemPosition();
                if (endCompletelyPosition == mHistoryAdapter.getItemCount() - 1) {
                    count = count + 8;
                    initData();
                }
            }
        });
    }

    @Override
    public int getLayoutView() {
        return R.layout.activity_history;
    }

    @Override
    protected void initData() {
        List<TomorrowData> date = getDate();
        if (date.size() > 0) {
            mData.addAll(date);
            mHistoryAdapter.notifyDataSetChanged();
        } else {
            Toast.makeText(mContext, "没有更多了，继续加油", Toast.LENGTH_SHORT).show();
        }
    }

    class HistoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new MyHolder(LayoutInflater.from(mContext).inflate(R.layout.history_item, null));
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof MyHolder) {
                TomorrowData data = mData.get(position);
                SimpleDateFormat yyyy = new SimpleDateFormat("yyyy年MM月dd日");
                ((MyHolder) holder).tvTime.setText(yyyy.format(new Date(data.getTimeStamp())));
                ((MyHolder) holder).tvContent.setText("今日一共完成了" + data.getCount() + "个番茄");
                ((MyHolder) holder).tvAllTime.setText(getStringDate(data.getAllTime()));
            }
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }
    }

    private String getStringDate(int allTime) {
        int hour = allTime / 60;
        int min = allTime % 60;
        String text = "一共完成了" + hour + "小时" + min + "分钟。继续加油中";
        return text;
    }

    class MyHolder extends RecyclerView.ViewHolder {
        TextView tvTime, tvContent, tvAllTime;
        ImageView ivShare;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            tvTime = itemView.findViewById(R.id.tv_time);
            tvContent = itemView.findViewById(R.id.tv_context);
            ivShare = itemView.findViewById(R.id.iv_share);
            tvAllTime = itemView.findViewById(R.id.tv_all_time);
        }
    }

    private List<TomorrowData> getDate() {
        List<TomorrowData> timeStampDesc = LitePal
                .order("timeStamp desc")
                .limit(8)
                .offset(count)
                .find(TomorrowData.class);
        return timeStampDesc;
    }
}
